/*
	File:		BaseMelee.cpp
	Author:		Ben Feldmann
	Purpose:	Class for managing base melee weapon
*/

#include "BaseMelee.h"
#include "ObjectManager.h"
#include "EventSystem.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"
#include "GamePlayState.h"


BaseMelee::BaseMelee(Player* owner)
{
	SetWidth(32);
	SetHeight(16);
	SetDamage(30.0f);

	SetOwner(owner);
	SetSpcDamage(50.0f);
	SetCoolTimer(0.0f);
	SetCoolSeconds(2.0f); // Test Variable
	SetAtkSpeed(0.45f);
	SetEnergyCost(10.0f);
	SetX(owner->GetX() + (owner->GetWidth()/2.0f));
	SetY(owner->GetY() + (owner->GetHeight()/2.0f));

	//fistAttack = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/fistAttack.wav"));

	ObjectManager::GetInstance()->AddObject(this);
	Release();

	type = OBJ_WEAPON;

	meleeType = MW_BASE;

	atkTimer = 0.0f;
	attacked = false;
	moveFist = false;
	isAttacking = false;
	isSpcAttacking = false;

	playerX = GetOwner()->GetX();
	playerY = GetOwner()->GetY();

}

BaseMelee::~BaseMelee()
{
	//if(fistAttack != -1)
	//{
	//	CSGD_XAudio2::GetInstance()->SFXStopSound(fistAttack);
	//	CSGD_XAudio2::GetInstance()->SFXUnloadSound(fistAttack);
	//	fistAttack = -1;
	//}

	ObjectManager::GetInstance()->RemoveObject(this);

	/*if(fistAttack != -1)
	{
		if(CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(fistAttack))
			CSGD_XAudio2::GetInstance()->SFXStopSound(fistAttack);

		CSGD_XAudio2::GetInstance()->SFXUnloadSound(fistAttack);
		fistAttack = -1;
	}*/
}

bool BaseMelee::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true && base != this->GetOwner() && base->GetType() != OBJ_PICKUP)
	{
		if(GetOwner()->GetClock() == false)
		{
			if(attacked == true && isAttacking == true)
			{
				float damage = GetDamage();
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(this->GetDamage())); 

				return true;
			}
			else
				return false;
		}
		else if(GetOwner()->GetClock() == true)
		{
			if(attacked == true && isAttacking == true)
			{
				float damage = GetDamage();
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(this->GetDamage()*1.5f)); 

				return true;
			}
			else
				return false;
		}
		else 
			return false;
	}
	else
		return false;
}

void BaseMelee::Attack() 
{
	if(isAttacking == false)
	{
		weaponAnimation[GetDirection()].Play();
		isAttacking = true;
		attacked = true;
	}
}

void BaseMelee::SpecialAttack() 
{
	if(spcAttacked == false && spcAttacking == false)
	{
		spcAttacking = true;
		spcAttacked = true;
	}
}

void BaseMelee::Update(float elapsed)
{
	if(isAttacking == true)
	{
		atkTimer += elapsed;

		if(atkTimer >= GetAtkSpeed())
		{
			isAttacking = false;
			attacked = false;
			atkTimer = 0.0f;
		}
	}

	SetDirection(GetOwner()->GetDirection());
	if(GetOwner()->GetAnimation() != &weaponAnimation[GetDirection()])
	{
		GetOwner()->SetAnimation(&weaponAnimation[GetDirection()]);
	}
	if(spcAttacked == true)
	{
		if(spcAttacking == true)
		{
			SetCoolSeconds(GetCoolSeconds()+elapsed);

			if(GetCoolSeconds() >= 1.0f)
			{
				spcAttacking = false;
				SetCoolTimer(0.0f);
			}
		}

		if(spcAttacking == false)
		{
			SetCoolSeconds(GetCoolSeconds()+elapsed);
			
			if(GetCoolSeconds() >= GetCoolTimer())
			{
				spcAttacked = false;
				SetCoolSeconds(0.0f);
			}
		}

	}

}

void BaseMelee::Render()
{
	//CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
	//CSGD_Direct3D::GetInstance()->DrawRect(GetRect(), 255, 0, 0);
}

RECT BaseMelee::GetRect()
{
	RECT rect = {};
	int dir = GetDirection();

	switch(dir)
	{
	case 0:
		{
			rect = MeleeWeapon::GetRect();
			rect.right += 10;
		}
		break;
	case 1:
		{
			rect = MeleeWeapon::GetRect();
			rect.left -= 10;
		}
		break;
	case 2:
		{
			rect = MeleeWeapon::GetRect();
		}
		break;
	case 3:
		{
			rect = MeleeWeapon::GetRect();
		}
		break;
	default:
		{
		}
		break;
	}
	return rect;
}
